from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, Text, DateTime
from sqlalchemy.orm import relation, backref, mapper

Base = declarative_base()
metadata = Base.metadata

# ==== TABLE ====
##news_table = Table('news', metadata,
##    Column('id', Integer, primary_key=True))
##
##word_table = Table('word', metadata,
##    Column('id', Integer, primary_key=True))

news_words_table = Table('news_words', metadata,
    Column('news_id', Integer, ForeignKey('news.news_id'), primary_key=True),
    Column('word_id', Integer, ForeignKey('word.word_id'), primary_key=True),
    Column('count', Integer)
    )

# ==== CLASS ====
class NewsWords(object):
    pass

class News(Base):
    __tablename__  = "news"

    news_id = Column(Integer, primary_key=True)
    link = Column(String(256))

    words = relation(NewsWords)

    def __init__(self, link):
        self.link = link

    def __repr__(self):
        return u"<News('%s')>" % (self.link)

class Word(Base):
    __tablename__  = "word"

    word_id = Column(Integer, primary_key=True)
    word = Column(String(96))

    def __init__(self, word):
        self.word = word

    def __repr__(self):
        return u"<Word('%s')>" % (self.word)


# ==== MAPPER ====
#mapper(Association, association_table)

##mapper(News, news_table, properties={
##    'children':relation(Association)
##})

mapper(NewsWords, news_words_table, properties={
    'word':relation(Word)
})

#mapper(Word, word_table)






